package com.lwl.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.entity.MyPage;
import com.lwl.entity.Tuser;
import com.lwl.dao.TuserMapper;
import com.lwl.service.ITuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Lei
 * @create 2022-10-19 12:33
 */
@Service
public class TuserServiceImpl implements ITuserService {

    @Autowired
    private TuserMapper tuserMapper;

    /**
     * 添加/修改用户信息
     * @param tuser
     * @return
     */
    @Override
    public Boolean registerUser(Tuser tuser) {
        int insert = -1;
        if (tuser.getId()==null){
            String salt = UUID.randomUUID().toString();
//          不需要额外的加密类
            String oldpwd=tuser.getPassword()+salt;
            String newpassword = DigestUtils.md5DigestAsHex(oldpwd.getBytes());
            tuser.setPassword(newpassword);
            tuser.setSalt(salt);
            tuser.setCreatetime(new Date());
            tuser.setUpdatetime(new Date());
            insert = tuserMapper.insert(tuser);
        }else {
            tuser.setUpdatetime(new Date());
//            updateByExampleSelective:根据id修改传递过来的实体类中有值的列，没有值的列设置为null
//            updateByPrimaryKeySelective:根据id修改传递过来的实体类中有值的列，没有值的列不做修改
            insert = tuserMapper.updateByPrimaryKeySelective(tuser);
        }
        return insert > 0?true:false;
    }

    /**
     *  查询所有数据，并进行分页
     */
    @Override
    public PageInfo queryAll(MyPage myPage,Tuser tuser) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<Tuser> tusers = tuserMapper.queryAll(tuser);
        PageInfo pageInfo = new PageInfo(tusers);
        return pageInfo;
    }

    /**
     * 根据id修改用户状态
     * @param tuser
     */
    @Override
    public boolean updUser(Tuser tuser) {
        System.out.println("要修改的用户信息为tuser = " + tuser);
        int i = tuserMapper.updateByPrimaryKeySelective(tuser);
        return i>0?true:false;
    }

    /**
     * 查询用户信息
     * @param username
     * @return
     */
    @Override
    public boolean queryByName(String username) {
        List<Tuser> tusers = tuserMapper.queryByName(username);
        int size = tusers.size();
        return size>0?false:true;
    }

    /**
     * 主页查询登录用户的基本资料
     * @param username
     * @return
     */
    @Override
    public Tuser queryByUserName(String username) {
        System.out.println("username = " + username);
        List<Tuser> tusers = tuserMapper.queryByUserName(username);
        Tuser tuser = tusers.get(0);
        System.out.println("查到的用户是tuser = " + tuser);
        return tuser;
    }

    /**
     * 主页修改用户密码
     */
    @Override
    public boolean updatePass(Tuser tuser, String newpassword) {
        System.out.println("tuser = " + tuser);
//        页面上传来的明文密码
        String password = tuser.getPassword();
//        页面上传来的用户名，根据用户名来查询数据库内容
        String username = tuser.getUsername();
        List<Tuser> tusers = tuserMapper.queryByName(username);
        System.out.println("tusers = " + tusers);
//        数据库中对应用户的密文密码
        String password1 = tusers.get(0).getPassword();
//          数据库中对应用户的盐
        String salt = tusers.get(0).getSalt();
//        使用页面密码加密,用来对比加密密码是否正确
        String newPassword = password + salt;
//        根据页面的密码和数据库的盐生成新的密码
//        String newpassword = DigestUtils.md5DigestAsHex(oldpwd.getBytes());
        String s = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        if (s.equals(password1)){
//            根据前端页面新的密码生成加密密码
            String s1 = newpassword + salt;
            String s2 = DigestUtils.md5DigestAsHex(s1.getBytes());
            //设置新的密文密码为用户密码
            tusers.get(0).setPassword(s2);
            int i = tuserMapper.updateByPrimaryKeySelective(tusers.get(0));
            return true;
//            int updstatus = tuserMapper.updatePass(s,username);
//            return updstatus>0?true:false;
        }else {
            return false;
        }
    }
}
